package com.zzs.client.controller;

import com.zzs.client.pojo.dto.submit.CodeSubmitDTO;
import com.zzs.client.pojo.vo.submit.CodeSubmitDetailVO;
import com.zzs.client.pojo.vo.submit.CodeSubmitVO;
import com.zzs.client.service.CodeSubmitService;
import com.zzs.common.core.enums.ResponseCode;
import com.zzs.common.core.response.PageResult;
import com.zzs.common.core.response.Result;
import com.zzs.common.core.util.ThreadLocalUtil;
import com.zzs.common.security.exception.ServiceException;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zzs
 * @date 2025/04/01
 */
@RestController
@RequestMapping("/client/submit")
public class CodeSubmitController {
    @Autowired
    private CodeSubmitService codeSubmitService;
    @PostMapping
    public Result<String> submit(@Valid @RequestBody CodeSubmitDTO codeSubmitDTO) {
        Long userId = ThreadLocalUtil.getUserId();
        if (userId == null) {
            throw new ServiceException(ResponseCode.UNAUTHORIZED);
        }
        String id = codeSubmitService.submit(codeSubmitDTO, userId);
        return Result.success(id);
    }

    @GetMapping("/{codeSubmitId}")
    public Result<CodeSubmitDetailVO> getCodeSubmitDetail(@PathVariable("codeSubmitId") long codeSubmitId) {
        CodeSubmitDetailVO codeSubmitDetail = codeSubmitService.getCodeSubmitDetail(codeSubmitId);
        return Result.success(codeSubmitDetail);
    }

    @GetMapping
    public Result<PageResult<CodeSubmitVO>> listCodeSubmits(@RequestParam("problemId") long problemId) {
        Long userId = ThreadLocalUtil.getUserId();
        if (userId == null) {
            throw new ServiceException(ResponseCode.UNAUTHORIZED);
        }
        PageResult<CodeSubmitVO> pageResult = codeSubmitService.listCodeSubmits(userId, problemId);
        return Result.success(pageResult);
    }
}
